package org.eae.eldf.webapp.controller.module;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.common.bean.Result;
import org.eae.eldf.webapp.common.bean.ResultList;
import org.eae.eldf.webapp.common.constant.StateDefine;
import org.eae.eldf.dto.module.ModuleMenuDto;
import org.eae.eldf.dto.module.ModuleMenuQueryDto;
import org.eae.eldf.entity.module.ModuleInfo;
import org.eae.eldf.entity.module.ModuleMenu;
import org.eae.eldf.entity.module.ModulePurview;
import org.eae.eldf.framework.annotation.Token;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.service.module.ModuleInfoService;
import org.eae.eldf.service.module.ModuleMenuService;
import org.eae.eldf.service.module.ModulePurviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;




@Controller
@RequestMapping("/module")
public class ModuleMenuController extends AbstractController<ModuleMenuDto>{
	
	private ModuleMenuService moduleMenuService;
	
	private ModuleInfoService moduleInfoService;
	
	private ModulePurviewService modulePurviewService;
	
	private EhCacheCacheManager cacheManager;
	
	@Autowired
	public void setModuleMenuService(ModuleMenuService moduleMenuService) {
		this.moduleMenuService = moduleMenuService;
	}
	@Autowired
	public void setCacheManager(EhCacheCacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}
	
	@Autowired
	public void setModuleInfoService(ModuleInfoService moduleInfoService) {
		this.moduleInfoService = moduleInfoService;
	}
	
	@Autowired
	public void setModulePurviewService(ModulePurviewService modulePurviewService) {
		this.modulePurviewService = modulePurviewService;
	}
	/**
	 * 查询所有menu
	 * @param searchValue
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/menu/list")
	public String initPage(@RequestParam(required = false) String searchValue,
			ModelMap model,
			@RequestParam(required = false) Long moduleMenuId,
			@RequestParam(required = false) Long parentId,
			@RequestParam(required = false) Long moduleInfoId,
			HttpServletRequest request, HttpServletResponse response){
		List<ModuleInfo> listModuleInfos = new ArrayList<ModuleInfo>();
		listModuleInfos = moduleMenuService.queryModuleList();
		
		
		model.addAttribute("moduleMenuId", moduleMenuId);
		model.addAttribute("moduleInfoId", moduleInfoId);
		model.addAttribute("list", listModuleInfos);
		return "/module/menu/module_menu";
		
	}
	/**
	 * grid  查询
	 * @param request
	 * @param response
	 * @param page
	 * @param id
	 * @param rows
	 * @param searchValue
	 * @return
	 */
	@RequestMapping(value = "/menu/list_json")
	public @ResponseBody ResultList<ModuleMenuDto> pageList(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(required = false) Long id,
			@RequestParam(required = false) Boolean isMeun,
			@ModelAttribute (value="moduleMenuQueryDto") ModuleMenuQueryDto moduleMenuQueryDto){
			/*Pagination<ModuleMenu> pager = super.initPagination(request);*/
			Pagination<ModuleMenuDto> pager = super.newPaginationInstance(moduleMenuQueryDto);
			if(moduleMenuQueryDto.getParentId() == null ||moduleMenuQueryDto.getParentId() == 0){
				isMeun = false;
				pager=moduleMenuService.pageMenuList(pager, moduleMenuQueryDto.getModuleInfoId(),isMeun);
			}else {
				isMeun = true;
				pager=moduleMenuService.pageMenuList(pager, moduleMenuQueryDto.getParentId(),isMeun);
			}
			
			
		return outPageArrayList(pager, moduleMenuQueryDto);
	}
	

	/**
	 * 下拉框展示选中模块的所有菜单
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/menu/listModuleInfos")
	public @ResponseBody 
	List<ModuleMenu> queryModuleList(@RequestParam Long id){
		ModuleInfo moduleInfo = moduleInfoService.queryById(id);
		List<ModuleMenu> listModuleInfos = new ArrayList<ModuleMenu>();
		listModuleInfos = moduleMenuService.queryModuleMenuListById(id);
		//String flag = "{'menuName':'"+moduleInfo.getModuleName()+"','parentId':-1,'moduleMenuId':0}";
		Map map = new HashMap();
		map.put("menuName", moduleInfo.getModuleName());
		map.put("parentId", -1);
		map.put("moduleMenuId", 0);
		map.put("moduleInfoFlag", id);
		
		/*new JSONObject(map);*/
		/*listModuleInfos.add(new JSONObject(map));*/
		List list = new ArrayList();
		list.addAll(listModuleInfos);
		list.add(map);
		return list;
		
	}
	
	/**
	 * 通过父级id查询子级
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/menu/listModules")
	public @ResponseBody 
	String queryModuleListByParentId(@RequestParam Long id){
		
		/*Criteria criteria = Criteria.create(ModuleMenu.class).where("parentId", new Object[] { id });
		List<ModuleMenu> listModules=moduleMenuService.queryList(criteria);
		System.out.println(listModules.size());
		if (listModules.size()==0) {
			return "isChild";
		}
			return "success";
		*/
		
		List<ModuleMenu> listModules = moduleMenuService.queryModuleListByParentId(id);
		if (listModules.size()==0) {
			return "isChild";
		}
			return "success";
		
	}
	
	/**
	 * 跳转到表单页面
	 * @param id
	 * @param model
	 * @param searchValue
	 * @param sorting_column_order
	 * @param sorting_asc_desc
	 * @param iDisplayStart
	 * @return
	 */
	@RequestMapping("/menu/form")
	@Token(save = true)
	public  String toFormPage(@RequestParam(required = false) Long id,
			ModelMap model,
			@RequestParam(required = false) String searchValue,
			@RequestParam(required = false) String parentId,
			@RequestParam(required = false) String moduleInfoId,
			@RequestParam(required = false) String moduleMenuId,
			@RequestParam(required = false) String sorting_column_order,
			@RequestParam(required = false) String sorting_asc_desc,
			@RequestParam(required = false) String iDisplayStart) {
		ModuleMenu moduleMenu = null;
		if (null != id) {
			moduleMenu = moduleMenuService.queryById(id);
		} else {
			moduleMenu = new ModuleMenu();
		}
		model.addAttribute("parentId", parentId);
		model.addAttribute("moduleInfoId", moduleInfoId);
		model.addAttribute("moduleMenuId", moduleMenuId);
		model.addAttribute("moduleMenu", moduleMenu);
		model.addAttribute("searchValue", searchValue);
		model.addAttribute("sorting_column_order", sorting_column_order);
		model.addAttribute("sorting_asc_desc", sorting_asc_desc);
		model.addAttribute("iDisplayStart", iDisplayStart);
		return "/module/menu/form";
	}
	
	/**
	 * 删除菜单
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/menu/delete")
	public @ResponseBody
	Result delete(@ModelAttribute(value="moduleMenu") ModuleMenu moduleMenu ) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		try {
			String isHasP = isHasPur(moduleMenu.getModuleMenuId());
			String isChild = queryModuleListByParentId(moduleMenu.getModuleMenuId());
			if(isHasP.equals("notHasPur") && isChild.equals("isChild")){
				moduleMenuService.delete(moduleMenu.getModuleMenuId());
			}else{
				result.setMessage("请选择子模块并且没有权限的模块");
				result.setResultValue(false);
				return result;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setMessage("删除失败");
			result.setResultValue(false);
			return result;
		}
		return result;
	}
	
	/**
	 * 新增菜单
	 * 
	 * @param operator
	 * @return
	 */
	@RequestMapping(value = "/menu/add", method = RequestMethod.POST)
	@ResponseBody
	//@Token(remove = true, repeatSubmitForwardURL = "/module/menu/form")
	public  Result insert(
			@RequestParam(required = false) Long moduleMenuId,

			@RequestParam(required = false) Long parentId,
			@RequestParam(required = false) Long moduleInfoId,
			@RequestParam(required = false) String I18nName,
			@ModelAttribute(value="moduleMenuDto") ModuleMenuDto moduleMenu,
			HttpServletRequest request, ModelAndView model) {
				Result result = new Result(StateDefine.FLAG_SUCCESS, true);
				
				moduleMenuService.insertMenu(moduleMenu);
				if(parentId !=null && parentId!=0L ){
				moduleMenuService.update(Criteria.create(ModuleMenu.class).set("isLeaf", 0)
						.where("moduleMenuId", new Object[] { parentId }));
				}
				model.addObject("success", true);
				model.addObject("moduleMenuId", moduleMenuId);
				model.addObject("id", moduleInfoId);
				model.addObject("moduleInfoId", moduleInfoId);
				
				return result;
			
		} 
	
	
	
	/**
	 * 编辑菜单
	 * @param id
	 * @param moduleMenuId
	 * @param nameKey
	 * @param menuName
	 * @param menuTitle
	 * @param isRoot
	 * @param isLeaf
	 * @param imagePath
	 * @param moudleIsDelete
	 * @param menuPath
	 * @param menuRemark
	 * @param sortNumber
	 * @param iconCls
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/menu/update", method = RequestMethod.POST)
	@ResponseBody
	public  Result update(@RequestParam(required = false) Long id,
			@RequestParam(required = false) Long moduleMenuId,
			@RequestParam(required = false) Long moduleInfoId,
			@RequestParam(required = false) Long parentId,
			@RequestParam(required = false) String nameKey,
			@RequestParam(required = false) String menuName,
			@RequestParam(required = false) String menuTitle,
			@RequestParam(required = false) Long isRoot,
			@RequestParam(required = false) Long isLeaf,
			@RequestParam(required = false) String imagePath,
			@RequestParam(required = false) String menuPath,
			@RequestParam(required = false) String menuRemark,
			@RequestParam(required = false) Long sortNumber,
			@RequestParam(required = false) String iconCls,
			HttpServletRequest request,
			@ModelAttribute(value="moduleMenu") ModuleMenuDto moduleMenu,
			 ModelAndView model) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);

		moduleMenuService.updateMenu(moduleMenu);
		
		model.addObject("success", true);
		model.addObject("moduleMenuId", moduleMenuId);
		model.addObject("moduleInfoId", moduleInfoId);
		return result;
	}
	/**
	 * 判断改菜单是否拥有权限
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/menu/isHasPur")
	public @ResponseBody
	String isHasPur(@RequestParam Long id) {
		try {
		Integer modulePurCount = modulePurviewService.queryCount(Criteria.create(ModulePurview.class).where("moduleMenuId", new Object[]{id}));
		/*System.out.println(modulePurCount);*/
		if (modulePurCount == 0) {
			return "notHasPur";
		}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "isHasPur";
	}
	
	/**
	 * 修改子菜单标识
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/menu/isLeaf")
	public @ResponseBody
	String isLeaf(@RequestParam Long id) {
		try {
			moduleMenuService.update(Criteria.create(ModuleMenu.class).set("isLeaf", 1)
					.where("moduleMenuId", new Object[] { id }));
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	/**
	 * 修改子菜单标识
	 * @param id
	 * @return
	 *//*
	@RequestMapping(value="/menu/isChildren")
	public @ResponseBody
	String isChildren(@RequestParam Long id) {
		try {
			moduleMenuService.queryModuleMenuListByParentId(id);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}*/
	
}
